package com.qqtech.qquan.homemaking.service;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.homemaking.dao.HomemakingBaseDao;
import com.qqtech.qquan.homemaking.dao.HomemakingItemDao;
import com.qqtech.qquan.homemaking.enums.HomemakingItemStatusDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingBase;
import com.qqtech.qquan.homemaking.model.HomemakingBaseQuery;
import com.qqtech.qquan.homemaking.model.HomemakingBaseVo;
import com.qqtech.qquan.homemaking.model.HomemakingItem;
import com.qqtech.qquan.homemaking.model.HomemakingItemQuery;

@Service
public class HomemakingBaseServiceImpl extends BaseServiceImpl<HomemakingBase>
		implements HomemakingBaseService {
	public static final Logger log = LoggerFactory.getLogger(HomemakingBaseServiceImpl.class);
	
	@Resource
	private HomemakingBaseDao homemakingBaseDao;
	
	@Resource
	private HomemakingItemDao homemakingItemDao;

	@Override
	protected BaseDao<HomemakingBase> getBaseDao() {
		return homemakingBaseDao;
	}

	@Override
	public int addHomemakingBase(HomemakingBase homemakingBase,
			List<HomemakingItem> items) {
		Timestamp now = TimeUtil.now();
		homemakingBase.setNewAt(now);
		homemakingBase.setUpdAt(now);
		int id = homemakingBaseDao.insert(homemakingBase);
		for (HomemakingItem item : items) {
			if (item.getItemId() != null) {
				item.setBaseId(id);
				item.setNewAt(now);
				item.setStatus(HomemakingItemStatusDictEnum.NORMAL.getCode());
				homemakingItemDao.insert(item);
			}
		}
		return id;
	}

	@Override
	public int updateHomemakingBase(HomemakingBase homemakingBase,
			List<HomemakingItem> items) {
		Timestamp now = TimeUtil.now();
		homemakingBase.setUpdAt(now);
		homemakingBaseDao.updateById(homemakingBase);
		HomemakingItemQuery iQuery = new HomemakingItemQuery();
		iQuery.setBaseId(homemakingBase.getId());
		List<HomemakingItem> itemList = homemakingItemDao.queryList(iQuery);
		for (HomemakingItem item : items) {
			boolean has = false;
			for (HomemakingItem old : itemList) {
				if (item.getItemId().intValue() == old.getItemId()) {
					item.setId(old.getId());
					item.setNewAt(null);
					item.setStatus(HomemakingItemStatusDictEnum.NORMAL.getCode());
					homemakingItemDao.updateById(item);
					itemList.remove(old);
					has = true;
					break;
				}
			}
			if (!has) {
				item.setBaseId(homemakingBase.getId());
				item.setNewAt(now);
				item.setStatus(HomemakingItemStatusDictEnum.NORMAL.getCode());
				homemakingItemDao.insert(item);
			}
		}
		// 没有选中的，统一设置为DELETE
		for (HomemakingItem temp : itemList) {
			temp.setStatus(HomemakingItemStatusDictEnum.DELETE.getCode());
			temp.setNewAt(null);
			homemakingItemDao.updateById(temp);
		}
		return 1;
	}

	@Override
	public Page<HomemakingBaseVo> queryPageListByType(HomemakingBase query,
			Pageable pageable) {
		return homemakingBaseDao.queryPageListByType(query, pageable);
	}

	@Override
	public int myHomemakingBaseCount(Integer userId) {
		int count = 0;
		if (userId != null) {
			HomemakingBaseQuery q = new HomemakingBaseQuery();
			q.setCreatorId(userId);
			count = homemakingBaseDao.queryCount(q);
		}
		return count;
	}

	@Override
	public Page<HomemakingBaseVo> queryPageListAdminByType(
			HomemakingBase query, Pageable pageable) {
		return homemakingBaseDao.queryPageListAdmin(query, pageable);
	}

	@Override
	public boolean hasAddHomemakingBaseByType(Integer userId, Integer typeId) {
		if (userId == null || typeId == null) {
			return false;
		} else {
			HomemakingBaseQuery q = new HomemakingBaseQuery();
			q.setCreatorId(userId);
			q.setTypeId(typeId);
			q.setIsShow(YesNoEnum.YES.getCode());
			int count = homemakingBaseDao.queryCount(q);
			if (count > 0) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int getCountByCanServise(HomemakingBase query) {
		if (query == null) {
			return 0;
		}
		return homemakingBaseDao.queryCountByCanServise(query);
	}

	@Override
	public Page<HomemakingBaseVo> queryPageListByCanServise(
			HomemakingBase query, Pageable pageable) {
		return homemakingBaseDao.queryPageListByCanServise(query, pageable);
	}
	
}